package mylab.starters.sms.support.aliyun;

import cn.hutool.core.util.StrUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import mylab.starters.sms.AbstractSmsTemplate;
import mylab.starters.sms.SmsProvider;
import mylab.starters.sms.config.SmsProperties;
import mylab.starters.sms.model.SmsData;
import mylab.starters.sms.model.SmsResponse;
import mylab.utils.common.jackson.JacksonUtil;
import mylab.utils.spring.spi.SpiProvider;

import java.util.Collection;

@Slf4j
public class AliyunSmsTemplate extends AbstractSmsTemplate<SmsProperties.AliyunSmsProperties> {

    private static final String successCode = "OK";

    public AliyunSmsTemplate(SmsProperties.AliyunSmsProperties properties) {
        super(properties);
    }

    @Override
    public SpiProvider getProvider() {
        return SmsProvider.Aliyun;
    }

    /**
     * 获取默认客户端
     *
     * @return
     */
    private IAcsClient getDefaultAcsClient(SmsProperties.AliyunSmsProperties properties) {
        DefaultProfile profile = DefaultProfile.getProfile(properties.getRegionId(), properties.getAccessKey(), properties.getSecretKey());

        DefaultProfile.addEndpoint(properties.getRegionId(), properties.getProduct(), properties.getDomain());

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        return new DefaultAcsClient(profile);
    }


    private SendSmsRequest request(SmsProperties.AliyunSmsProperties properties, SmsData smsData, Collection<String> phoneNumbers) {
        SendSmsRequest request = new SendSmsRequest();
        request.setSysMethod(MethodType.POST);
        request.setPhoneNumbers(StrUtil.join(",", phoneNumbers));
        request.setSignName(properties.getSignName());
        request.setTemplateCode(smsData.getTemplateId());
        request.setTemplateParam(JacksonUtil.toJsonString(smsData.getParams()));
        return request;
    }


    @Override
    public SmsResponse sendSms(SmsData smsData, Collection<String> phoneNumbers) {
        IAcsClient acsClient = getDefaultAcsClient(properties);
        SendSmsRequest request = request(properties, smsData, phoneNumbers);

        SmsResponse smsResponse = new SmsResponse();

        try {
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (null != sendSmsResponse && !StringUtils.isEmpty(sendSmsResponse.getCode())) {
                if (successCode.equals(sendSmsResponse.getCode())) {
                    smsResponse.setSuccess(true);
                } else {
                    log.error("Send Aliyun Sms Fail: [code={}, message={}]", sendSmsResponse.getCode(), sendSmsResponse.getMessage());
                }
                smsResponse.setCode(sendSmsResponse.getCode());
                smsResponse.setMessage(sendSmsResponse.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            smsResponse.setMessage("Send Aliyun Sms Fail!");
        }
        return smsResponse;
    }
}
